//
//  HLPushContentView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLPushContentView.h"
#import "HLPushAttachment.h"
#import "HLIMKitMessageModel.h"
#import <NIMSDK/NIMMessage.h>

@implementation HLPushContentView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    
    self.imageView = [UIImageView.alloc init];
    [self addSubview:self.imageView];
    
    self.titleLabel = [UILabel.alloc init];
    self.titleLabel.textColor = [UIColor colorWithRed:36/255.0 green:41/255.0 blue:51/255.0 alpha:1.0];
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    [self addSubview:self.titleLabel];
    
    self.contentLabel = [UILabel.alloc init];
    self.contentLabel.textColor = [UIColor colorWithRed:168/255.0 green:171/255.0 blue:185/255.0 alpha:1.0];
    self.contentLabel.font = [UIFont systemFontOfSize:14];
    self.contentLabel.numberOfLines = 0;
    [self addSubview:self.contentLabel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.width;
    
    NIMCustomObject *object = (NIMCustomObject *)self.messageModel.message.messageObject;
    HLPushAttachment *attachment = (HLPushAttachment *)object.attachment;
    
    CGFloat height = 0;
    
    if (attachment.messageImageUrl.length) {
        self.imageView.frame = CGRectMake(0, 0, width, width * attachment.imageHeight / attachment.imageWidth) ;
        height = CGRectGetMaxY(self.imageView.frame);
    }
    
    if (attachment.messageTitle) {
        height += 16;
        self.titleLabel.frame = CGRectMake(16, height, width - 16*2, 22);
        height = CGRectGetMaxY(self.titleLabel.frame);
    }
    
    if (attachment.messageContent) {
        height += 4;
        CGFloat msgHeight = [attachment.messageContent boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
        self.contentLabel.frame = CGRectMake(16, height, width - 16*2, msgHeight);
    }
}

- (void)refreshData:(HLIMKitMessageModel *)messageModel
{
    [super refreshData:messageModel];
    
    self.bubbleImageView.image = [UIImage qmui_imageWithColor:UIColor.whiteColor];
    
    NIMCustomObject *object = (NIMCustomObject *)messageModel.message.messageObject;
    HLPushAttachment *attachment = (HLPushAttachment *)object.attachment;
    if (attachment.messageImageUrl.length) {
        self.imageView.hidden = NO;
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:attachment.messageImageUrl] placeholderImage:[UIImage imageNamed:@""]];
    } else {
        self.imageView.hidden = YES;
    }
    if (attachment.messageTitle) {
        self.titleLabel.hidden = NO;
        self.titleLabel.text = attachment.messageTitle;
    } else {
        self.titleLabel.hidden = YES;
    }
    
    if (attachment.messageContent) {
        self.contentLabel.hidden = NO;
        self.contentLabel.text = attachment.messageContent;
    } else {
        self.contentLabel.hidden = YES;
    }
}

@end
